﻿using System;
using Selenium;
using Xunit;

namespace JoelAbrahamsson.Web.Tests.UITests
{
    public class CommentTests : IDisposable
    {
        private ISelenium selenium;

        public CommentTests()
        {
            SeleniumServer.Start();

            selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://local.joelabrahamsson.com/");
            selenium.Start();
        }

        public void Dispose()
        {
            selenium.Stop();

            SeleniumServer.Stop();
        }

        [Fact]
        public void ValidCommentInptut_NewCommentPosted_CommentIsDisplayed()
        {
            string authorName = Guid.NewGuid().ToString();
            string authorUrl = "http://" + Guid.NewGuid();
            string comment = Guid.NewGuid().ToString();

            //Open the first page
            selenium.Open("/");

            //Click on the link to the latest post
            selenium.Click("css=#entryTeaserList a:first-child");
            selenium.WaitForPageToLoad("30000");

            //Post a new comment
            selenium.Type("authorName", authorName);
            selenium.Type("authorEmail", "test@test.com");
            selenium.Type("authorUrl", authorUrl);
            selenium.Type("comment", comment);
            selenium.Click("subscribeToComments");
            selenium.Click("//input[@value='Add Comment']");
            selenium.WaitForPageToLoad("30000");

            Assert.True(selenium.IsElementPresent("link=" + authorName), "No link with the commenters name found");
            Assert.True(selenium.IsElementPresent(string.Format("css=a[href=\"{0}\"]", authorUrl)), "No link to the commenters url found");
            Assert.True(selenium.IsTextPresent(comment), "The comment text was not displayed");
        }
    }
}
